<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    @close="handleClose"
    @open="handleOpen"
  >
    <!-- 表单 -->
    <el-form label-width="120px">
      <el-form-item label="姓名">
        <el-input
          v-model="empForm.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机">
        <el-input
          v-model="empForm.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker
          v-model="empForm.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="聘用形式">
        <el-select
          v-model="empForm.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireTypes"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号">
        <el-input
          v-model="empForm.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门">
        <el-input
          v-model="empForm.departmentName"
          readonly
          style="width: 50%"
          placeholder="请选择部门"
          @focus="showTree = true"
        />
        <!-- 部门树形组件 -->
        <el-tree
          v-show="showTree"
          :data="deptTreeData"
          :props="{ label: 'name' }"
          @node-click="handleNodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker
          v-model="empForm.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleAdd">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 导入常量模块
import empAppoint from '@/constant/employees'
import { getDeptListAPI } from '@/api/departments'
import { list2Tree } from '@/utils/list2tree'
import { addEmpolueeAPI } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 聘用形式列表
      hireTypes: empAppoint.hireType,
      //  部门树形结构
      deptTreeData: '',
      showTree: false,
      //   员工信息表单
      empForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    }
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      this.$emit('update:showDialog', false)
    },
    // 弹窗打开时
    async handleOpen() {
      const resp = await getDeptListAPI()
      // 转为树形结构再赋值
      this.deptTreeData = list2Tree(resp.data.depts, '')
    },
    // 树形节点点击时
    handleNodeClick(node) {
      // node: 被点击的部门(节点)对象
      //   如果有子部门，不赋值
      if (node.children) return
      this.empForm.departmentName = node.name
      // 隐藏窗口
      this.showTree = false
    },
    async handleAdd() {
      try {
        // 整体校验
        // 发起新增请求
        await addEmpolueeAPI(this.empForm)
        // 关闭弹框
        this.handleClose()
        // 获取员工列表
        //   this.$parent.getEmpioyeeList()
        //  推荐子传父
        this.$emit('add-success')
        //   成功提示
        this.$message.success('添加成功')
      } catch (error) {
        if (error.message) this.$message.error(error.message)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
